import java.util.Arrays;

public class Practice {
    public static void main1(String[] args) {
        int[] array = {1, 2, 3, 4, 5};

        //创建数组时不指定数据个数的静态定义数组
        int[] array2 = new int[]{1, 2, 3, 4, 5};

        //动态定义数组
        int[] array3 = new int[10];
        System.out.println(Arrays.toString(array3));//把数组以字符串形式输出

        boolean[] flgs = new boolean[5];
        System.out.println(Arrays.toString(flgs));


    }

    public static void main2(String[] args) {
        int[] array = {1, 2, 3, 4, 5};
        //正常fori打印数组
        for (int i = 0; i < array.length; i++) {
            System.out.print(array[i]);
            System.out.print(" ");
        }
        System.out.println(" ");

        //for each打印，需要通过一个变量来接收
        for (int x : array) {
            System.out.print(x + " ");
        }
        System.out.println(" ");

        String ret = Arrays.toString(array);
        System.out.println(ret);
    }

    public static void main3(String[] args) {
        int[] array = null;
        System.out.println(Arrays.toString(array));
        //System.out.println(array.length);  报错：空指针异常
    }

    public static void main4(String[] args) {
        int[] array1 = new int[3];
        array1[0] = 10;
        array1[1] = 20;
        array1[2] = 30;

        int[] array2 = new int[]{1, 2, 3, 4, 5};

        array2[0] = 100;
        array2[1] = 200;

        array1 = array2;

        array1[2] = 300;
        array1[3] = 400;
        array1[4] = 500;
        //请问两个数组输出结果为多少
        System.out.println(Arrays.toString(array1));
        System.out.println(Arrays.toString(array2));
    }

    public static void print(int[] array) {
        array[0] = 99;
        for (int i = 0; i < array.length; i++) {
            System.out.print(array[i] + " ");
        }
        System.out.println();
    }

    public static void func1(int[] array) {
        array = new int[]{1, 2, 3};//修改了形参的指向
    }

    public static void func2(int[] array) {
        array[0] = 199;//修改了实参的值
    }

    public static void main7(String[] args) {
        int[] array = {10, 20, 30};
        func1(array);
        //func2(array);
        System.out.println(Arrays.toString(array));
        //传引用不是一定会修改实参的值，要看具体拿到引用之后，形参做了什么动作
    }

    public static void main8(String[] args) {
        int[] array = {10, 20, 30};
        print(array);
    }

    //实现自己的myToString
    public static String myToString(int[] array) {
        String str = "[";
        for (int i = 0; i < array.length; i++) {
            str = str + array[i];
            if (i != array.length - 1) {
                str = str + ", ";
            }
        }

        str = str + "]";

        return str;
    }

    public static void main9(String[] args) {
        int[] array = {1, 2, 3, 4};
        String ret = myToString(array);
        System.out.println(ret);
    }

    //拷贝copy
    public static int[] copyof(int[] array) {
        int[] copy = new int[array.length];
        for (int i = 0; i < array.length; i++) {
            copy[i] = array[i];
        }
        return copy;
    }

    public static void main10(String[] args) {
        int[] array = {1, 2, 3, 4, 5, 6, 7, 8, 9};
        int[] copy = new int[array.length];
        //1，java自带的拷贝
        /**
         * 参数1：是拷贝的数组
         * 参数2：是要拷贝的长度
         */
        int[] copy1 = Arrays.copyOf(array, 2 * array.length);
        int[] copy2 = Arrays.copyOfRange(array, 2, 5);

        //(原数组：array,复制长度：从0开始,目标数组：copy,粘贴：从0开始)
        System.arraycopy(array, 0, copy, 0, array.length);

        System.out.println(Arrays.toString(copy));
    }

    //数组相加求平均值
    public static void main11(String[] args) {
        int[] array = {1, 2, 3, 4, 5, 6, 7, 8, 9};
        int sum = 0;
        for (int x : array
        ) {
            sum += x;
        }
        System.out.println(sum * 1.0 / array.length);//平均值可能不是一个整数
    }


    public static void main12(String[] args) {
        int[] array1 = {1, 2, 3, 4, 5, 6, 7, 18, 9};
        int[] array2 = {1, 2, 3, 4, 5, 6, 7, 18, 9, 20};

        boolean flg = Arrays.equals(array1, array2);//判断两个数组是否相等，返回值为布尔类型
        System.out.println(flg);

        int[] array3 = new int[10];
        Arrays.fill(array3, 3, 5, -1);//填充函数，(填充目标数组，填充开始位置，填充结束位置，填充值)
        System.out.println(Arrays.toString(array3));
    }

    //交换接口
    public static void swap(int[] array, int i, int j) {
        int tmp = array[i];
        array[i] = array[j];
        array[j] = tmp;
    }

    //反转升序数组
    public static void reverse(int[] array) {
        int left = 0;
        int right = array.length - 1;
        while (left < right) {
            swap(array, left, right);
            left++;
            right--;
        }
    }

    public static void main13(String[] args) {
        int[] array = {1, 2, 3, 4, 5, 6};
        reverse(array);
        System.out.println(Arrays.toString(array));
    }

    //冒泡排序
    public static void bubbleSort(int[] array) {
        //i 表示比较的趟数
        for (int i = 0; i < array.length - 1; i++) {
            for (int j = 0; j < array.length - 1 - i; j++) {

                if (array[j + 1] > array[j]) {
                    swap(array, j, j + 1);
                }

            }

        }
    }

    //二分法
    public static int binarySearch(int[] array, int key) {
        int left = 0;
        int right = array.length - 1;
        while (left <= right) {
            int mid = (left + right) / 2;
            if (array[mid] < key) {
                left = mid + 1;
            } else if (array[mid] > key) {
                right = mid - 1;
            } else {
                return mid;
            }
        }
        return -1;
    }

    public static void func01(int[] array){
        array = new int[10];//将形参引用指向另一个对象，并在函数执行完之后删除这个引用
    }

    public static void func02(int[] array){
        array[0] = 99;//通过形参引用指向实参的引用，直接修改实参引用对象的值
    }

    //改变原有数组的值
    public static void transform(int[] array){
        for (int i = 0; i < array.length; i++) {
            array[i] *= 2;
        }
    }


    //若不能在原数组之上进行改变，则使用transform2
    public static int[] transform2(int[] array){
        int[] tmp = new int[array.length];
        for (int i = 0; i < array.length; i++) {
            tmp[i] = 2*array[i];
        }
        return  tmp;
    }

    public static void main14(String[] args) {
        int[] array = {1,2,3};
        int[] ret = transform2(array);
        transform(array);
        System.out.println(Arrays.toString(array));
        System.out.println(Arrays.toString(ret));

    }


    //奇数位于偶数之前
    public static void func3(int[] array){
        int left = 0;
        int right = array.length-1;
            while (left<right) {
                while (left < right && array[right] % 2 == 0) {
                    right--;
                }
                while (left < right && array[left] % 2 != 0){
                    left++;
                }
                swap(array,left,right);
            }
        }


    public static void main15(String[] args) {
        int[] array = {1,2,3,4,5};
        func3(array);
        System.out.println(Arrays.toString(array));
    }

    //寻找两数之和的序列号
    public static int[] func4(int[] array , int target){
        int[] ret = new int[]{-1,-1};
        for (int i = 0; i < array.length-1; i++) {
            for (int j = 1; j < array.length; j++) {
                if (array[i] + array[j] == target){
                   ret[0] = i;
                   ret[1] = j;
                }
            }
        }
        return ret;
    }

    public static void main16(String[] args) {
        int[] array = {1,2,3,4,5,6,7,8};
        int[] num =func4(array,8);
        System.out.println(Arrays.toString(num));
    }

    //找数组中只出现过一次的数字
    public static int func5(int[] array){
        int result = 0;
        for (int i = 0; i < array.length; i++) {
            result ^= array[i];
        }
        return result;
    }

    public static void main17(String[] args) {
        int array[] = {1,2,2,3,3,1,4};
        System.out.println(func5(array));
    }

    //找多数元素
    public static int func6(int[] array){
        Arrays.sort(array);
        return array[array.length/2];
    }

    public static void main18(String[] args) {
        int[] array = {1,2,2,2,1};
        int ret = func6(array);
        System.out.println(ret);
    }

    //连续三个数是奇数
    public static boolean func7(int[] array){
        int count = 0;
        for (int i = 0; i < array.length; i++) {
            if (array[i] % 2 != 0){
                count++;
            } else {
                count = 0;
            }
            if (count == 3){
                break;
            }
        }
        if(count == 3){
            return true;
        }
        else {
            return false;
        }
    }
    public static void main19(String[] args) {
        int[] array1 = {2,6,4,1};
        int[] array2= {1,2,34,3,4,5,7,23,12};
        boolean flag1 = func7(array1);
        boolean flag2 = func7(array2);
        System.out.println(flag1);
        System.out.println(flag2);
    }

    //二维数组的定义
    public static void main(String[] args) {
        int[][] array1 = {{1,2,3},{4,5,6}};
        int[][] array2 = new int[][]{{1,2,3},{4,5,6}};
        int[][] array3 = new int[2][3];//两行三列
        int[][] array4 = new int[2][];//java里面可以省略列，c语言可以省略行，但是如果再像下面这样打印出来会报空指针异常，由此引出我们的不规则二维数组
        array4[0] = new int[3];
        array4[1] = new int[5];

        //array3[1].length == 3  ;   array3.length == 2 (引用类型);
        //下面打印这个二维数组
        for (int i = 0; i < array1.length ; i++) {
            for (int j = 0; j < array1[i].length; j++) {
                System.out.print(array1[i][j]+" ");
            }
            System.out.println(" ");
        }

        for (int i = 0; i < array4.length ; i++) {
            for (int j = 0; j < array4[i].length; j++) {
                System.out.print(array4[i][j]+" ");
            }
            System.out.println(" ");
        }
    }

}
